﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Report.BrowserFix;
using Report.Entity;
using Report.Properties;
using Report.Service;

namespace Report.UI
{
    public partial class BrowserForm : Form
    {
        private readonly GomezReportExporter exporter = new GomezReportExporter();

        public BrowserForm()
        {
            InitializeComponent();
            gomezWebBrowser.CanGoBackChanged += GomezWebBrowserCanGoBackChanged;
            gomezWebBrowser.CanGoForwardChanged += GomezWebBrowserCanGoForwardChanged;
            ToolBarPanel.Visible = Settings.Default.ToolBar;
        }

        public void ReportButton_OnClick(object sender, HtmlElementEventArgs e)
        {
            if (!exporter.Started)
            {
                exporter.PrepareContext(gomezWebBrowser);
            }

            exporter.ExecuteCurrentStep();

            ShowHtmlReportInfo(gomezWebBrowser.Url.OriginalString);

            MessageBox.Show(this, exporter.Started ? Resources.StepOk : Resources.ProcessOk);
        }

        private void SkiInputClick(object sender, HtmlElementEventArgs e)
        {
            exporter.MoveToNextStep();
            ShowHtmlReportInfo(null);
        }

        #region WebBrowser Event

        private void GomezWebBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (gomezWebBrowser.Document == null) return;

            if (e.Url.OriginalString == "about:blank") return;

            ShowHtmlReportInfo(e.Url.OriginalString);
        }

        private void ShowHtmlReportInfo(string url)
        {
            string stepName = null;
            if (exporter.Started)
            {
                stepName = exporter.GetCurrentStpName();
            }
            else
            {
                ImageSetting imageSetting = ImageSettingManager.Instance.ReadSettingByUrl(url);
                if (imageSetting != null)
                {
                    stepName = string.Format("{0}-{1}", imageSetting.Name, imageSetting.Steps[0].Name);
                }
            }

            if (string.IsNullOrEmpty(stepName)) return;

            HtmlDocument document = gomezWebBrowser.Document;

            //Create Div
            HtmlElement divElement = document.GetElementById("Report-{D5CF7150-3511-40c4-A6F6-2292F64549BA}");
            if (divElement == null)
            {
                divElement = document.CreateElement("div");
                divElement.Id = "Report-{D5CF7150-3511-40c4-A6F6-2292F64549BA}";
                divElement.Style = "position:absolute; z-index:100;right:30px; top:105px; left:auto; bottom:auto;";
                gomezWebBrowser.Document.Body.AppendChild(divElement);
            }
            //Append Create Button
            HtmlElement createElement = document.GetElementById("Create-{D5CF7150-3511-40c4-A6F6-2292F64549BA}");
            if (createElement == null)
            {
                createElement = document.CreateElement("input");
                createElement.SetAttribute("type", "button");
                createElement.Id = "Create-{D5CF7150-3511-40c4-A6F6-2292F64549BA}";
                createElement.Click += ReportButton_OnClick;
                divElement.AppendChild(createElement);
            }

            //Append Skip Button
            HtmlElement skipElement = document.GetElementById("Skip-{D5CF7150-3511-40c4-A6F6-2292F64549BA}");

            if (skipElement == null)
            {
                skipElement = document.CreateElement("input");
                skipElement.SetAttribute("type", "button");
                skipElement.SetAttribute("value", "Skip current step");
                skipElement.Id = "Skip-{D5CF7150-3511-40c4-A6F6-2292F64549BA}";
                skipElement.Click += SkiInputClick;
                divElement.AppendChild(skipElement);
            }

            createElement.SetAttribute("value", stepName);
        }

        private void GomezWebBrowserCanGoForwardChanged(object sender, EventArgs e)
        {
            NextButton.Enabled = gomezWebBrowser.CanGoForward;
        }

        private void GomezWebBrowserCanGoBackChanged(object sender, EventArgs e)
        {
            backButton.Enabled = gomezWebBrowser.CanGoBack;
        }

        private void GomezWebBrowserNavigating(object sender, WebBrowserNavigatingEventArgs e)
        {
        }

        private void GomezWebBrowserNavigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            urlTextBox.Text = gomezWebBrowser.Url.ToString();
        }

        private void BackButtonClick(object sender, EventArgs e)
        {
            gomezWebBrowser.GoBack();
        }

        private void NextButtonClick(object sender, EventArgs e)
        {
            gomezWebBrowser.GoForward();
        }

        private void RefreshButtonClick(object sender, EventArgs e)
        {
            gomezWebBrowser.Refresh();
        }

        private void UrlTextBoxKeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(urlTextBox.Text))
                {
                    gomezWebBrowser.Navigate(urlTextBox.Text);
                }
            }
        }

        private void HomtButtonClick(object sender, EventArgs e)
        {
            gomezWebBrowser.GoHome();
        }

        private void GomezWebBrowserBeforeNewWindow(object sender, EventArgs e)
        {
            var eventArgs = e as WebBrowserExtendedNavigatingEventArgs;
            gomezWebBrowser.Navigate(eventArgs.Url);
            eventArgs.Cancel = true;
        }

        #endregion

        private void SettingButtonClick(object sender, EventArgs e)
        {
            SettingForm settingForm=new SettingForm();
            settingForm.ShowDialog(this);
        }
    }
}